<?php
/**
 * @舆情类
 * 1、舆情类别列表
 * 2、舆情列表
 * 3、舆情详情
 * 4、舆情栏目
 *
 * @作者
 * 万全强（php）
 */
namespace app\controller;

use app\controller\HomeBase;
use think\facade\Db;

use app\model\OpinionsTypeModel;
use app\model\OpinionsModel;


class Opinions extends HomeBase
{
	
	private $OpinionsModel;
	private $OpinionsTypeModel;
	
	public function __construct(){
		
		parent::__construct();
		$this->OpinionsTypeModel=new OpinionsTypeModel();	
		$this->OpinionsModel=new OpinionsModel();	
	}
	/**
	 * 舆情类别列表
	 *
	 * @return void
	 */
	public function GetOpinionsTypeList()
	{
		$param=request()->param();
		$res=$this->OpinionsTypeModel->GetList();
		return json(['status'=>1,'msg'=>'获取成功','data'=>$res]);
	}
	/**
	 * 舆情列表
	 *
	 * @return void
	 */
	public function GetOpinionsList()
	{
		$param=request()->param();
		$where="1=1 and status !=-1";
		if(isset($param['keywords'])&&!empty($param['keywords'])){
			$where.=" and title like '%".trim($param['keywords'])."%'";
		}
		if(isset($param['status'])&&strlen($param['status'])>0){
			$where.=" and status = '{$param['status']}'";
		}
		if(isset($param['type_id'])&&!empty($param['type_id'])){
			$where.=" and type_id = '{$param['type_id']}'";
		}
		if(isset($param['chain_id'])&&!empty($param['chain_id'])){
			$where.=" and FIND_IN_SET('{$param['chain_id']}',chain_id) ";
		}
		if(isset($param['today'])&&!empty($param['today'])){
			$today=strtotime(date('Y-m-d 00:00:01'));
			$where.=" and updatetime>=$today ";
		}
		
		
		$res=$this->OpinionsModel->GetList($where);
		return json(['status'=>1,'msg'=>'获取成功','data'=>$res]);
	}
	/**
	 * 舆情详情
	 *
	 * @return void
	 */
	public function GetOpinionsDetail()
	{
		$param=request()->param();
		$id=isset($param['id'])?intval($param['id']):0;
		$result=$this->OpinionsModel->detail($id);
		
		return json(['status'=>1,'msg'=>'获取成功','data'=>$result]);
		
	}
	/**
	 * 舆情添加/编辑
	 *
	 * @return void
	 */
	public function OpinionsEdit()
	{
		$param=request()->param();
		$id=isset($param['id'])?intval($param['id']):0;
		
		//表单验证
		try {
			$re=$this->validate($param,'app\validate\OpinionsValidate');
		} catch (\Exception $e) {
			return json(['status'=>0,'msg'=>$e->error,'data'=>[]]);
		}
		
		//管理表
		if($id){
			$id= $this->OpinionsModel->EditData($param,$id);
		}else{
			$param['role_id']=$this->role_id;
			$id= $this->OpinionsModel->AddData($param);
		}
		
		return json(['status'=>1,'msg'=>'操作成功','data'=>['id'=>$id]]);
	}
	/**
	 * 舆情删除
	 *
	 * @return void
	 */
	public function OpinionsDel()
	{
		$param=request()->param();
		$id=isset($param['id'])?trim($param['id']):'';
		
		$result=$this->OpinionsModel->Del(explode(',',$id));
		if($result){
			return json(['status'=>1,'msg'=>'删除成功','data'=>[]]);
		}else{
			return json(['status'=>0,'msg'=>'删除失败','data'=>[]]);
		}
	}
	/**
	 * 舆情是否开启禁用
	 *
	 * @return void
	 */
	public function UpdateOpinionsStatus()
	{
		$param=request()->param();
		$id=isset($param['id'])?intval($param['id']):0;
		
		$result=$this->OpinionsModel->UpdateStatus($id);
		if($result){
			return json(['status'=>1,'msg'=>'操作成功','data'=>[]]);
		}else{
			return json(['status'=>0,'msg'=>'操作失败','data'=>[]]);
		}
	}
	
}
